/*
 * @lc app=leetcode.cn id=111 lang=csharp
 *
 * [111] 二叉树的最小深度
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
    public int MinDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        var maxDepth = 0;
        var thisLevelCount = 0;
        Queue<TreeNode> queue = new Queue<TreeNode>();
        queue.Enqueue(root);
        while(queue.Count > 0){
            maxDepth++;
            thisLevelCount = queue.Count;
            for(int i = 0; i < thisLevelCount; i++){
                TreeNode node = queue.Dequeue();
                if(node.left == null && node.right == null){
                    return maxDepth;
                }
                if(node.left != null){
                    queue.Enqueue(node.left);
                }
                if(node.right != null){
                    queue.Enqueue(node.right);
                }
            }
        }
        return maxDepth;
    }
}
// @lc code=end

